Skip to content

[Updated PR] [Feature] Autocomplete context from other tabs open in the editor #6012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jun 11, 2025

Conversation

adarshramiyer
Copy link
Collaborator

Description

Adds context to autocomplete using files open in the editor. Logs open files and viewing order in an LRU cache, then prioritizes fitting recent and shorter files into the context window. Adaptively trims files to fit within prompt token limits as necessary.

Checklist

  • I've read the contributing guide
  • The relevant docs, if any, have been updated or created
  • The relevant tests, if any, have been updated or created

…reak from this branch to test changeVisibleTextEditors
@adarshramiyer adarshramiyer requested a review from a team as a code owner June 6, 2025 00:13
@adarshramiyer adarshramiyer requested review from RomneyDa and removed request for a team June 6, 2025 00:13
Copy link

cubic-dev-ai bot commented Jun 6, 2025

Your cubic subscription is currently inactive. Please reactivate your subscription to receive AI reviews and use cubic.

Copy link

netlify bot commented Jun 6, 2025

Deploy Preview for continuedev canceled.

Name Link
🔨 Latest commit 93ed462
🔍 Latest deploy log https://app.netlify.com/projects/continuedev/deploys/68486e082d35560008196cc9

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 6, 2025
Copy link
Collaborator

@RomneyDa RomneyDa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean implementation!

Some changes in mind:

  1. Use the existing files/closed or files/opened messages rather than introducing a new onDidCloseTextDocument callback and initializeOpenedFileCache message. "files/closed" is already set up for both vs code and jetbrains. It seems like in this implementation, recentlyEdited is for open files and recentlyOpened is for closed files, but recentlyOpened is also initialized with open files. Let's talk about this naming and review the snippet priority. I think recently edited should be prioritized over recently closed. Also, let's implement the existing files/opened message rather than adding a new initializeOpenedFileCache message. It seems like those should be added to the recently edited cache on startup, not the recently "opened" (closed) cache.
  2. Use URIs all the way through, don't use "file://", will cause bugs, just use uri.toString() in the IDE side . Note that helper.filepath was named before we transitioned to URIs, bit misleading
  3. Let's add unit tests for the formatting and ranking, especially around the math stuff!

I also wanted to talk to you about snippet ranking, deduplication, and file read latency, can chat in person, you probably already tested this enough but wanted to get a grasp on it. Did we consider storing the actual file contents in the cache? Might not be up to date but would be much faster and these are for "closed" files. Files are often large, is there a concern of recently closed files always filling the remainder of snippet context window?

Were the additions/changes to manual-testing-sandbox intentional? Fine to be included, just checking

EDIT:
after talking with @adarshramiyer feedback has changed to

  1. Use the existing "files/closed" message rather than a onDidCloseTextDocument callback (partly so works with jetbrains), move logic to core files/closed
  2. Instead of a initializeOpenedFileCache message, send a "files/opened" message on init, switch onDidChangeActive to using the "files/opened"
  3. Rip out the recentlyEditedFiles cache since it's duplicate
  4. Use URIs all the way through, no fsPath or "file://" templating
  5. Promise.all for parallel file reads, and race each one to maybe 80 ms, something a bit less than 100
  6. Deduplication of snippets with e.g. recently visited ranges, but can be done on a future PR

let openFilePaths = vscode.window.tabGroups.all // get tab groups
.flatMap((group) => group.tabs) // extract tabs from tab groups
.filter((tab) => tab.input instanceof vscode.TabInputText) // filter irrelevant tabs (settings, etc.)
.map((tab) => (tab.input as vscode.TabInputText).uri.fsPath); // get filepaths
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the existing VsCodeIDEUtils getOpenFiles function. Looks like you might be able to improve it by using this TabInputText approach instead of the existing documentIsCode filter. Make sure to use uri.toString() instead of fsPath

}

try {
const fileContent = await ide.readFile(fileUri);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was the max cache size decided, by file read latency? It seems like with this setup it might often not finish reading all files in the cache within 100 ms, e.g. with 20 files could take 200+. Consider using Promise.all()

core/core.ts Outdated
async ({ data: { initialOpenedFilePaths } }) => {
try {
for (const filepath of initialOpenedFilePaths) {
openedFilesLruCache.set(`file://${filepath}`, `file://${filepath}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove file://, use URI from IDE all the way through


// Returns linear combination of recency and size scores
// recency score is exponential decay over recency; log normalized score is used for size
const getRecencyAndSizeScore = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be awesome for unit tests

@github-project-automation github-project-automation bot moved this from Todo to In Progress in Issues and PRs Jun 6, 2025
@dosubot dosubot bot removed the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 9, 2025
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Jun 9, 2025
Copy link
Collaborator

@RomneyDa RomneyDa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great new tests!
Also I think might be an issue with useRecentlyOpened setting - how can it be set, what is the default? JSON only for now?

core/core.ts Outdated
@@ -597,14 +600,56 @@ export class Core {
});

on("files/closed", async ({ data }) => {
try {
const filepaths = data.fileUris;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for fileUris and uris, same thing

@@ -76,6 +78,7 @@ export class VsCodeExtension {
},
);
this.ide = new VsCodeIde(this.webviewProtocolPromise, context);
this.ideUtils = new VsCodeIdeUtils();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this.ide.getOpenFiles and merge the utils, no need for new util

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 11, 2025
@adarshramiyer adarshramiyer merged commit 3f595ec into main Jun 11, 2025
37 checks passed
@adarshramiyer adarshramiyer deleted the feature/view-context/base-copy branch June 11, 2025 23:25
@github-project-automation github-project-automation bot moved this from In Progress to Done in Issues and PRs Jun 11, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants